package com.stonedt.spider.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stonedt.spider.dao.IStonedtUserDao;
import com.stonedt.spider.entity.StonedtUser;
import com.stonedt.spider.service.IStonedtUserService;
import com.stonedt.spider.util.MD5Util;
import com.stonedt.spider.util.ResultUtil;

@Service()
public class IStonedtUserServiceImpl implements IStonedtUserService {
	@Autowired
	private IStonedtUserDao stonedtUserDao;

	/**
	 * 登录验证
	 */
	@Override
	public StonedtUser cheakUser(String username) {
		// TODO Auto-generated method stub
		return stonedtUserDao.cheakUser(username);
	}

	/**
	 * 登录验证
	 */
	@Override
	public List<StonedtUser> queryUserAll() {
		// TODO Auto-generated method stub
		return stonedtUserDao.queryUserAll();
	}

	/**
	 * 修改用户状态
	 */
	@Override
	public Integer modifyState(String username, Integer status) {
		// TODO Auto-generated method stub
		return stonedtUserDao.modifyState(username, status);
	}

	/**
	 * 删除用户
	 */
	@Override
	public Integer delStonedtUser(String username) {
		// TODO Auto-generated method stub
		return stonedtUserDao.delStonedtUser(username);
	}

	/**
	 * 重置密码
	 */
	@Override
	public Integer resetPwd(String username, String password) {
		// TODO Auto-generated method stub
		return stonedtUserDao.resetPwd(username, password);
	}

	/**
	 * 添加用户
	 */
	@Override
	public Integer insertUser(StonedtUser user) {
		// TODO Auto-generated method stub
		return stonedtUserDao.insertUser(user);
	}
	
	/**
	 * 确认用户是否存在
	 */
	@Override
	public ResultUtil ifUserExist(String username) {
		// TODO Auto-generated method stub
		Integer result = stonedtUserDao.ifUserExist(username);
		ResultUtil resultUtil = new ResultUtil();
		if(result == 0){
			resultUtil.setStatus(200);
		}else{
			resultUtil.setStatus(500);
		}
		return resultUtil;
	}

	@Override
	public Integer selectUserCount() {
		// TODO Auto-generated method stub
		return stonedtUserDao.selectUserCount();
	}

	@Override
	public ResultUtil checkPassword(String password, Integer user_id) {
		// TODO Auto-generated method stub
		StonedtUser user = stonedtUserDao.selectUserByIdAndPassword(MD5Util.getMD5(password),user_id);
		ResultUtil result = new ResultUtil();
		if(null == user){
			result.setStatus(500);
		}else{
			result.setStatus(200);
		}
		return result;
	}

	@Override
	public ResultUtil repassword(String password, Integer user_id, String repassword) {
		// TODO Auto-generated method stub
		StonedtUser user = stonedtUserDao.selectUserByIdAndPassword(MD5Util.getMD5(password),user_id);
		ResultUtil result = new ResultUtil();
		if(null == user){
			result.setStatus(500);
		}else{
			try {
				user.setUser_password(MD5Util.getMD5(repassword));
				stonedtUserDao.updatePassword(user);
				result.setStatus(200);
			} catch (Exception e) {
				// TODO: handle exception
				result.setStatus(500);
			}
		}
		return result;
	}



}
